package vng.survey.model;

import com.google.gson.Gson;
import com.vng.zing.lib.Config;
import hapax.Template;
import hapax.TemplateDataDictionary;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.json.simple.JSONObject;

/**
 *
 * @author vuln2
 */
public class CommonModel {
    
    public static  final int OFFSET = Config.getParamInt("paging","offset");

    public static String getHeader(TemplateDataDictionary headerVariable, String title) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("header");
        headerVariable.setVariable("PAGE_TITLE", title);
        content = template.renderToString(headerVariable);
        return content;

    }
//        public static String setError(TemplateDataDictionary headerVariable, String error) throws Exception {
//		String content;
//		Template template = RenderModel.getCTemplate("error");
//                 headerVariable.setVariable("ERROR", error);
//		content = template.renderToString(headerVariable);
//		return content;
//                
//                
//	}

    public static String toJSON(String error, String msg) {
        Map mapdata = new HashMap();
        JSONObject ldata = new JSONObject();
        mapdata.put("error", error);
        mapdata.put("msg", msg);
        ldata.putAll(mapdata);
        return ldata.toJSONString();
    }

    public static String getFooter(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("footer");

        content = template.renderToString(footerVariable);
        return content;
    }
      public static String getError(TemplateDataDictionary footerVariable,String value) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("error");
        footerVariable.setVariable("ERROR", value);
        content = template.renderToString(footerVariable);
        return content;
    }
      public static String getPaging(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("paging");

        content = template.renderToString(footerVariable);
        return content;
    }
       public static String getNewTable(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("new_table");
        content = template.renderToString(footerVariable);
        return content;
    }
        public static String getHotTable(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("hot_table");
        content = template.renderToString(footerVariable);
        return content;
    }

    public static String getTextAnswer(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("text_answer");
        content = template.renderToString(footerVariable);
        return content;
    }

    public static String getParagraphAnswer(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("paragraph_answer");
        content = template.renderToString(footerVariable);
        return content;
    }

    public static String getSingleChoiceAnswer(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("single_choice_answer");
        content = template.renderToString(footerVariable);
        return content;
    }

    public static String getMultipleChoiceAnswer(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("multiple_choice_answer");
        content = template.renderToString(footerVariable);
        return content;
    }

    public static String getStatTable(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("stat_table");
        content = template.renderToString(footerVariable);
        return content;
    }

    public static String getResponseTable(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("res_detail_table");
        content = template.renderToString(footerVariable);
        return content;
    }
    public static String getStatChart(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("chart_answer");
        content = template.renderToString(footerVariable);
        return content;
    }
    public static String getChartFunciton(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("chart_google_func");
        content = template.renderToString(footerVariable);
        return content;
    }
     public static String getStatMutilLine(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("stat_multi_line");
        content = template.renderToString(footerVariable);
        return content;
    }
     public static String getEmpty(TemplateDataDictionary footerVariable) throws Exception {
        String content;
        Template template = RenderModel.getCTemplate("empty");
        content = template.renderToString(footerVariable);
        return content;
    }
  

    public static Object toOBJECT(String JSONString, Type type) {
        Gson gson = new Gson();
        return gson.fromJson(JSONString, type);
    }

    public static String toJSON(Object o) {
        Gson gson = new Gson();
        return gson.toJson(o);
    }

    public static String getDate(int time) {
        Date date = new Date();
        date.setTime(new Long(time + "000"));
        SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
        return ft.format(date);
    }
   
   public static int getUniqueDate() {
        try {
            String pattern = "dd/MM/yyyy";
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            Date now = new Date();
            String strNow = format.format(now);
            return (int) (format.parse(strNow).getTime() / 1000);
        } catch (ParseException ex) {
            return -1;
        }


    }
    public static int getUniqueBeforeDate() {
           return getUniqueDate()-24*3600;
    }

}
